package com.navinfo.opentsp.platform.computing.analysis.entity;

import com.google.protobuf.MessageLite;
import org.apache.kafka.common.serialization.Serializer;

import java.io.UnsupportedEncodingException;
import java.util.Map;

public class ProtoBufSerializer implements Serializer{

    @Override
    public void configure(Map map, boolean b) {

    }

    @Override
    public byte[] serialize(String s, Object o) {
        if(o instanceof MessageLite){
            return  ((MessageLite)o).toByteArray();
        } if(o instanceof String){
            try {
                return String.valueOf(o).getBytes("UTF-8");
            } catch (UnsupportedEncodingException e) {
                return null;
            }
        } if(o instanceof byte[]) {
            return (byte[])o;
        } else {
            return null;
        }
    }

    @Override
    public void close() {

    }
}